package io.github.edwinliu.anynet;

import android.app.Activity;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    static String TAG = "AnyNet";
    RadioGroup mRgNettype;
    CheckBox mCbVpn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRgNettype=findViewById(R.id.rgFakeNet);
        mCbVpn=findViewById(R.id.cbFakeVpn);
        mCbVpn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG," onCheckedChanged:"+ isChecked);
                saveSharePrefs(ProviderUtils.KEY_NAME_VPN,isChecked);
                //SocketController.setVpn(isChecked);
                ProviderUtils.getInstance(MainActivity.this).setValue(ProviderUtils.KEY_NAME_VPN,""+isChecked);
            }
        });

        mRgNettype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d(TAG," onCheckedChanged:"+ checkedId);
                int type=AnyNet.NETWORK_TYPE_NONE;
                switch (checkedId)
                {
                    case R.id.rbNone:
                        type=AnyNet.NETWORK_TYPE_NONE;
                        break;

                    case R.id.rbMobile:
                        type=AnyNet.NETWORK_TYPE_MOBILE;
                        break;

                    case R.id.rbWifi:
                        type=AnyNet.NETWORK_TYPE_WIFI;
                        break;
                }
                saveSharePrefs(ProviderUtils.KEY_NAME_NETTYPE,type);
                //SocketController.setType(type);
                ProviderUtils.getInstance(MainActivity.this).setValue(ProviderUtils.KEY_NAME_NETTYPE,""+ type);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences mSharedPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);

        //boolean vpn=ProviderUtils.getInstance(this).getBooleanValue(ProviderUtils.KEY_NAME_VPN);
        boolean vpn=mSharedPreferences.getBoolean(ProviderUtils.KEY_NAME_VPN,false);
        mCbVpn.setChecked(vpn);
        //int nettype=ProviderUtils.getInstance(this).getIntValue(ProviderUtils.KEY_NAME_NETTYPE);
        int nettype=mSharedPreferences.getInt(ProviderUtils.KEY_NAME_NETTYPE,0);
        switch (nettype)
        {
            case 0:
                mRgNettype.check(R.id.rbNone);
                break;

            case 1:
                mRgNettype.check(R.id.rbMobile);
                break;

            case 2:
                mRgNettype.check(R.id.rbWifi);
                break;
        }

    }

    private void saveSharePrefs(String key, boolean val) {
       // ProviderUtils.getInstance(MainActivity.this).setValue(key,""+val);

        SharedPreferences mSharedPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mSharedPreferences.edit();
        mEditor.putBoolean(key, val).apply();
    }

    private void saveSharePrefs(String key, int val) {
       // ProviderUtils.getInstance(MainActivity.this).setValue(key,""+val);
        SharedPreferences mSharedPreferences = getSharedPreferences("config", Activity.MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mSharedPreferences.edit();
        mEditor.putInt(key, val).apply();

    }


}
